package com.sunchp.thrift.sample;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class Client {

    public static final int SERVER_PORT = 8081;
    public static final int TIMEOUT = 30000;
    public static TTransport transport = null;

    public static HelloService.Client client;

    public static void initClient() {
        try {
            transport = new TFramedTransport(new TSocket(getLocalIp(), SERVER_PORT, TIMEOUT));
            transport.open();
            TProtocol protocol = new TBinaryProtocol(transport);
            client = new HelloService.Client(protocol);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void destroyClient() {
        if (null != transport)
            transport.close();
    }


    public static String getLocalIp() {
        String ipAddress = null;
        Enumeration<NetworkInterface> net = null;
        try {
            net = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }

        while (net.hasMoreElements()) {
            NetworkInterface element = net.nextElement();
            Enumeration<InetAddress> addresses = element.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress ip = addresses.nextElement();
                if (ip instanceof Inet4Address) {

                    if (ip.isSiteLocalAddress()) {

                        ipAddress = ip.getHostAddress();
                    }

                }

            }
        }
        return ipAddress;
    }

    public static void main(String[] args) throws TException {
        initClient();
        System.out.println(client.sayHello("sunchp"));
        System.out.println(client.sayBye("sunchp"));
        destroyClient();
    }
}

